package com.hjjt.baselib.util

import android.content.Context
import com.hjjt.baselib.provider.BaseContentContextProvider

/**
 *
 * @ProjectName:    IntelligentTerminal
 * @Package:        com.hjiot.viewlib.util
 * @ClassName:      Extension
 * @Description:    尺寸转换工具
 * @Author:         孙浩
 * @CreateDate:     2022/6/15 9:08
 */
object DisplayUtils {
    /**
     *
     * convert px to its equivalent dp
     *
     * 将px转换为与之相等的dp
     *
     */
    fun px2dp(context: Context, pxValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (pxValue / scale + 0.5f).toInt()
    }

    /**
     *
     * convert dp to its equivalent px
     *
     * 将dp转换为与之相等的px
     *
     */
    fun dp2px(context: Context, dipValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dipValue * scale + 0.5f).toInt()
    }

    /**
     *
     * convert px to its equivalent sp
     *
     * 将px转换为sp
     *
     */
    fun px2sp(context: Context, pxValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (pxValue / fontScale + 0.5f).toInt()
    }

    /**
     *
     * convert sp to its equivalent px
     *
     * 将sp转换为px
     *
     */
    fun sp2px(context: Context, spValue: Float): Int {
        val fontScale = context.resources.displayMetrics.scaledDensity
        return (spValue * fontScale + 0.5f).toInt()
    }
}

fun Int.DP(): Int = DisplayUtils.dp2px(BaseContentContextProvider.mContext!!, this.toFloat())